import axios from 'axios';

// 创建axios实例
const api = axios.create({
  baseURL: 'http://localhost:8081/erat/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 请求拦截器
api.interceptors.request.use(
  config => {
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  response => {
    const res = response.data;
    // 后端可能返回 {code:"200", message:"操作成功", data:{}}
    if (res && res.code !== undefined) {
      if (res.code === 200 || res.code === '200' || res.code === 0 || res.code === '0') {
        return res.data;
      }
      // 非成功码
      const msg = res.message || '请求失败';
      console.error('接口错误：', msg);
      return Promise.reject(new Error(msg));
    }
    // 若无code字段则直接返回
    return res;
  },
  error => {
    console.error('网络错误：', error);
    return Promise.reject(error);
  }
);

// 班级相关API
export const classApi = {
  // 获取班级列表
  getClasses() {
    return api.get('/classes');
  },
  
  // 获取班级详情
  getClass(id) {
    return api.get(`/classes/${id}`);
  },
  
  // 创建班级
  createClass(data) {
    return api.post('/classes', data);
  },
  
  // 更新班级
  updateClass(id, data) {
    return api.put(`/classes/${id}`, data);
  },
  
  // 删除班级
  deleteClass(id) {
    return api.delete(`/classes/${id}`);
  },
  
  // 获取班级学生列表
  getStudents(classId) {
    return api.get(`/classes/${classId}/students`);
  },
  
  // 添加学生
  addStudent(classId, data) {
    return api.post(`/classes/${classId}/students`, data);
  },
  
  // 导入学生
  importStudents(classId, file) {
    const formData = new FormData();
    formData.append('file', file);
    return api.post(`/classes/${classId}/students/import`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
  },
  
  // 删除学生
  deleteStudent(classId, studentId) {
    return api.delete(`/classes/${classId}/students/${studentId}`);
  }
};

// 课程相关API
export const courseApi = {
  // 获取课程列表
  getCourses() {
    return api.get('/courses');
  },
  
  // 获取课程详情
  getCourse(id) {
    return api.get(`/courses/${id}`);
  },
  
  // 创建课程
  createCourse(data) {
    return api.post('/courses', data);
  },
  
  // 更新课程
  updateCourse(id, data) {
    return api.put(`/courses/${id}`, data);
  },
  
  // 删除课程
  deleteCourse(id) {
    return api.delete(`/courses/${id}`);
  }
};

// 统计分析相关API
export const statisticsApi = {
  // 统计实验报告提交情况 (GET)
  analyzeSubmissions(params) {
    const { reportFolderPath, ...rest } = params;
    const finalParams = { ...rest, folderPath: reportFolderPath };
    return api.get('/statistics', { params: finalParams });
  },
  // 导出学生维度统计结果
  exportStudent(params) {
    const { reportFolderPath, ...rest } = params;
    const finalParams = { ...rest, folderPath: reportFolderPath };
    return api.get('/statistics/export/student', {
      params: finalParams,
      responseType: 'blob'
    });
  },
  // 导出实验维度统计结果
  exportExperiment(params) {
    const { reportFolderPath, ...rest } = params;
    const finalParams = { ...rest, folderPath: reportFolderPath };
    return api.get('/statistics/export/experiment', {
      params: finalParams,
      responseType: 'blob'
    });
  }
};

export default api; 